use mylib::maps::*;//私有地图库
use termion::*;
use std::io::Read;

///常量列表
const SIZE: (usize, usize) = (22,52);//地图尺寸，20行，每行50像素块，1像素块2个英文字符大小
const LONG: usize = 5;//初始蛇长
const FOOD: usize = 3;//果实个数
//游戏主体
pub fn games() {
    let mut maps = Maps::new(SIZE.0,SIZE.1);//定义并初始化像素地图结构体数组
    let mut stdin = async_stdin().bytes();//定义输入句柄
    maps.set_snake(LONG)//生成长度为LONG的蛇
        .set_food(FOOD)//生成FOOD个果
        .show_welcome()//显示欢迎画面
        .sleep();//休眠
    loop{
        let c = stdin.next();//获取输入
        match c {//执行输入判断
            Some(Ok(b'q')) | Some(Ok(b'\x1B')) => break,
            Some(Ok(b'a')) => maps.judge_to_turn(1),
            Some(Ok(b'd')) => maps.judge_to_turn(3),
            Some(Ok(b'w')) => maps.judge_to_turn(2),
            Some(Ok(b's')) => maps.judge_to_turn(4),
            _ => &mut maps
        };
        match maps.do_move() {//移动
            true  => {maps.sleep();break;}//退出
            false => {maps.show().sleep();}//打印当前帧
        }
    }
}
